package com.hyts.mvc.register;

import com.hyts.mvc.base.wrapper.FilterWrapper;
import com.hyts.mvc.bean.FilterRegistrationBean;
import com.hyts.mvc.config.MvcValue;
import com.hyts.scanner.utility.StringUtils;
import lombok.Builder;
import lombok.extern.slf4j.Slf4j;

import javax.servlet.DispatcherType;
import javax.servlet.Filter;
import javax.servlet.FilterRegistration;
import java.util.EnumSet;
import java.util.List;

/**
 * project-name:coffice-toolbox
 * package-name:com.hyts.mvc.register
 * author:Libo/Alex
 * create-date:2019-07-02 17:44
 * email:liboware@gmail.com
 * description:此类主要用于:filter过滤器注册模式wrapper包装类
 */
@Builder
@Slf4j
public class FilterRegistationWrapper extends FilterWrapper {

    /**
     * 注册addFilter服务
     * @param filterName
     * @param filterClass
     * @return
     */
    protected boolean addFilter(String filterName,Class<? extends Filter> filterClass){
        try {
            FilterRegistration.Dynamic filterRegistration = getServletContext().addFilter(filterName,filterClass);
            filterRegistration.addMappingForUrlPatterns(EnumSet.of(DispatcherType.REQUEST,
                    DispatcherType.FORWARD,DispatcherType.INCLUDE),true, MvcValue.DEFAULT_VIEW_FILTER_PATH);
        } catch (Exception e) {
            log.error("注册Filter过滤器对象失败！",e);
            return false;
        }
        return true;
    }

    protected boolean addFilter(String filterName,Class<? extends Filter> filterClass,String urlMappping){
        try {
            FilterRegistration.Dynamic filterRegistration = getServletContext().addFilter(filterName,filterClass);
            filterRegistration.addMappingForUrlPatterns(EnumSet.of(DispatcherType.REQUEST,
                    DispatcherType.FORWARD,DispatcherType.INCLUDE),true,urlMappping);
        } catch (Exception e) {
            log.error("注册Filter过滤器对象失败！",e);
            return false;
        }
        return true;
    }

    protected boolean addFilter(String filterName,Class<? extends Filter> filterClass,String urlMappping,String... servletName){
        try {
            FilterRegistration.Dynamic filterRegistration = getServletContext().addFilter(filterName,filterClass);
            filterRegistration.addMappingForServletNames(EnumSet.of(DispatcherType.REQUEST,
                    DispatcherType.FORWARD,DispatcherType.INCLUDE),true,servletName);
            filterRegistration.addMappingForUrlPatterns(EnumSet.of(DispatcherType.REQUEST,
                    DispatcherType.FORWARD,DispatcherType.INCLUDE),true,urlMappping);
        } catch (Exception e) {
            log.error("注册Filter过滤器对象失败！",e);
            return false;
        }
        return true;
    }

    @Override
    public void addFilter(FilterRegistrationBean filterRegistration) {
        //判断是否进行servlet绑定
        String isBindServlet = "0",isUrlPath = "0";
        if(filterRegistration.getServletName() == null || filterRegistration.getServletName().length == 0){
            isBindServlet = "1";
        }
        if(StringUtils.isEmpty(filterRegistration.getUrlPath())){
            isUrlPath = "1";
        }
        //判断是否进行绑定服务
        switch (isBindServlet+isUrlPath){
            case "00":
                 addFilter(filterRegistration.getFilterName(),
                        filterRegistration.getFilterClass(),
                        filterRegistration.getUrlPath(),
                        filterRegistration.getServletName());
                 break;
            case "01":
                addFilter(filterRegistration.getFilterName(),
                        filterRegistration.getFilterClass(),
                        MvcValue.DEFAULT_VIEW_FILTER_PATH,
                        filterRegistration.getServletName());
                break;
            case "10":
                addFilter(filterRegistration.getFilterName(),
                        filterRegistration.getFilterClass(),
                        filterRegistration.getUrlPath());
                break;
            case "11":
                addFilter(filterRegistration.getFilterName(),
                        filterRegistration.getFilterClass(),
                        MvcValue.DEFAULT_VIEW_FILTER_PATH);
                break;
        }
    }

    @Override
    public void addFilter(List<FilterRegistrationBean> filterRegistration) {
        filterRegistration.parallelStream().forEach(param->{
            addFilter(param);
        });
    }
}
